<template>
  <div class="potential-edit">
    <el-dialog
      v-bind="$attrs"
      :title="state.dialogTitle"
      v-model="state.modalVisible"
      :before-close="cancelDialog"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="false"
      class="potential-dialog"
      draggable
    >
      <el-form ref="potentialRef" :model="state.form" :rules="state.rules" label-width="130px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="关联账号id" prop="qkaccountid">
              <el-input v-model="state.form.qkaccountid" placeholder="关联账号id" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="潜客uuid" prop="qkuuid">
              <el-input v-model="state.form.qkuuid" placeholder="潜客uuid" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="潜客ip地址" prop="qkip">
              <el-input v-model="state.form.qkip" placeholder="潜客ip地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="潜客终端类型" prop="qkzdlx">
              <el-input v-model="state.form.qkzdlx" placeholder="潜客终端类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="潜客系统版本号" prop="qkxtbbh">
              <el-input v-model="state.form.qkxtbbh" placeholder="潜客系统版本号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="潜客手机代号" prop="qksjdh">
              <el-input v-model="state.form.qksjdh" placeholder="潜客手机代号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="潜客手机型号" prop="qksjxh">
              <el-input v-model="state.form.qksjxh" placeholder="潜客手机型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="潜客gps位置" prop="qkgps">
              <el-input v-model="state.form.qkgps" placeholder="潜客gps位置" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属国家" prop="qkssgj">
              <el-input v-model="state.form.qkssgj" placeholder="所属国家" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属省" prop="qkssshen">
              <el-input v-model="state.form.qkssshen" placeholder="所属省" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所实市" prop="qkssshi">
              <el-input v-model="state.form.qkssshi" placeholder="所实市" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import usePotentialManageStore from '@store/modules/potentialManage'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'

const potentialManageStore = usePotentialManageStore()
const {proxy} = getCurrentInstance()
const potentialRef = ref(null)

const state = reactive({
  modalVisible: false,
  dialogTitle: '',
  editView: {},
  form: {
    qkaccountid: '',
    qkuuid: '',
    qkip: '',
    qkzdlx: '',
    qkxtbbh: '',
    qksjdh: '',
    qksjxh: '',
    qkgps: '',
    qkssgj: '',
    qkssshen: '',
    qkssshi: ''
  },
  rules: {
    // account: [{ required: true, message: '账号不能为空', trigger: 'blur' }],
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  }
})

const emits = defineEmits(['refresh'])

const getDataView = (qkid) => {
  nextTick(async () => {
    const result = await potentialManageStore.view({'qkid.Value': qkid})
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
  })
}

const submitDialog = () => {
  potentialRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        state.form.qkid = state.editView.qkid
        await potentialManageStore.up(state.form)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        await potentialManageStore.add(state.form)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      potentialRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

const cancelDialog = () => {
  state.editView = {}
  potentialRef.value.resetFields()
  state.modalVisible = false
}

const openDialog = async (data = {}) => {
  if (Object.keys(data).length) await getDataView(data.qkid)
  state.modalVisible = true
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.qkid}】` : '新增'
}

defineExpose({
  openDialog
})
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
